<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/3/13/013
 * Time: 16:13
 */

namespace app\project\controller;


use app\common\controller\Common;
use think\Controller;
use think\Db;
use think\Exception;
use think\Session;

class Comment extends Controller
{
    public function _initialize()
    {
        $swidthStyle = Session::get("style");
        $this->assign("swidthStyle",$swidthStyle);
        $pro_id = empty($_GET['pro_id'])?0: $_GET['pro_id'];
        $this->assign("pro_id",$pro_id);

        $library = empty($_GET['library'])?0:$_GET['library'];
        $this->assign("library",$library);
    }


    //推进进化详情
    public function planbasic(){

        //查询推进计划详情
        $id = $_GET['id'];
        $where['id'] = $id;
        $detail = Db::name("project_pro_plan")->where($where)->find();


        $field = [
            "plan_title"=>['name'=>"年份","type"=>"text"],
            "plan_content"=>['name'=>"重要工作节点推进计划","type"=>"text"],
            "plan_investment"=>['name'=>"本年计划投资（万元）","type"=>"text"],
        ];

        $this->assign("field",$field);


        $this->assign("detail",$detail);
        return $this->fetch("planbasic");
    }

    //基本信息
    public function basic(){
        //查询项目基本信息
        $id = $_GET['id'];
        $where['id'] = $id;
        $detail = Db::name("project_pro")->where($where)->find();

        //查询项目类别
        if(!empty($detail['project_type'])){
            $projectTypeName = Db::name("project_type")->where(['id'=>$detail['project_type']])->value("name");
        }

        //查询企业类型
        if(!empty($detail['enterprise'])){
            $enterprise = Db::name("project_type")->where(['id'=>$detail['enterprise']])->value("name");
        }

        //查询所属产业
        if(!empty($detail['IndustrialChain'])){
            $IndustrialChain = Db::name("project_type")->where(['id'=>$detail['IndustrialChain']])->value("name");
        }
        $detail['IndustrialChain'] = empty($IndustrialChain)?"":$IndustrialChain;
        $detail['enterprise'] = empty($enterprise)?"":$enterprise;
        $detail['project_type'] = empty($projectTypeName)?"":$projectTypeName;


        if(!empty($detail['overdueImage'])){
            $detail['overdueImage'] = explode(";",$detail['overdueImage']);
        }

        $start_status = [1=>"待开工",2=>"已开工",3=>"已逾期",4=>"已终止"];
        $detail['start_status'] = empty($start_status[$detail['start_status']])?"":$start_status[$detail['start_status']];
        $record = [1=>"已备案",2=>"已立项",3=>"未完成"];
        $contract = [1=>"已完成",2=>"未完成"];
        $scene = [1=>"有",2=>"无"];
        $detail['record'] = empty($record[$detail['record']])?"":$record[$detail['record']];
        $detail['contract'] = empty($contract[$detail['contract']])?"":$contract[$detail['contract']];
        $detail['scene'] = empty($scene[$detail['scene']])?"":$scene[$detail['scene']];
        $project_status = [0=>'基础开工',1=>'已竣工',2=>'主体开工'];
        $detail['project_status'] = empty($project_status[$detail['project_status']])?"":$project_status[$detail['project_status']];
        $reply = [1=>"正在申报",2=>"申报退回",3=>"成功申报"];
        $detail['reply'] = empty($reply[$detail['reply']])?"":$reply[$detail['reply']];
        $operation = [1=>"正常",2=>"异常"];
        $detail['operation'] = empty($operation[$detail['operation']])?"":$operation[$detail['operation']];

        $warehousing = [1=>"待入库",2=>"已入库"];
        $detail['warehousing'] = empty($warehousing[$detail['warehousing']])?"":$warehousing[$detail['warehousing']];

        $this->assign("detail",$detail);

        $field = [
            1=>[
                "title"=>['name'=>"项目名称","type"=>"text"],
                "content"=>['name'=>"主要建设内容及规模","type"=>"text"],
                "investment"=>['name'=>"总额度（万元）","type"=>"text"],
                "address"=>['name'=>"拟选址","type"=>"text"],
                "Investor"=>['name'=>"投资人","type"=>"text"],
                "cardMeeting"=>['name'=>"通过论证会时间(第几次批改)","type"=>"text"],
                "IndustrialChain"=>['name'=>"所属产业","type"=>"text"],
                "signRemark"=>['name'=>"需提请协调事项","type"=>"text"],
                "duty_unit"=>['name'=>"责任单位","type"=>"text"],
                "duty_person"=>['name'=>"责任人","type"=>"text"],
                "overdue"=>['name'=>"逾期类别","type"=>"text"],
                "overdueRemark"=>['name'=>"逾期说明","type"=>"text"],
                "overdueImage"=>['name'=>"逾期文件","type"=>"image"],
                "project_chief"=>['name'=>"业主联系人","type"=>"text"],
                "project_phone"=>['name'=>"联系人电话","type"=>"text"]
            ],
            2=>[
                "title"=>['name'=>"项目名称","type"=>"text"],
                "content"=>['name'=>"主要建设内容及规模","type"=>"text"],
                "investment"=>['name'=>"总额度（万元）","type"=>"text"],
                "address"=>['name'=>"选址","type"=>"text"],
                "start_status"=>['name'=>"开工状态","type"=>"text"],
                "prepareRemark"=>['name'=>"需提请协调事项","type"=>"text"],
                "duty_unit"=>['name'=>"责任单位","type"=>"text"],
                "duty_person"=>['name'=>"责任人","type"=>"text"],
                 "project_chief"=>['name'=>"业主联系人","type"=>"text"],
                "project_phone"=>['name'=>"联系人电话","type"=>"text"]
            ],
            3=>[
                "title"=>['name'=>"项目名称","type"=>"text"],
                "code"=>['name'=>"项目代码","type"=>"text"],
                "content"=>['name'=>"主要建设内容及规模","type"=>"text"],
                "investment"=>['name'=>"总额度（万元）","type"=>"text"],
                "address"=>['name'=>"选址","type"=>"text"],
                "warehousing"=>['name'=>"入库状态","type"=>"text"],
                "project_status"=>['name'=>"项目状态","type"=>"text"],
                "startRemark"=>['name'=>"需提请协调事项","type"=>"text"],
                "duty_unit"=>['name'=>"责任单位","type"=>"text"],
                "duty_person"=>['name'=>"责任人","type"=>"text"],
                "arrange_onstream_time"=>['name'=>"合作协议约定开工时间（招商新征地项目按取得土地时间后延一个月确定）","type"=>"text"],
                "onstream_time"=>['name'=>"实际开工时间","type"=>"text"],
                "arrange_complete_time"=>['name'=>"合作协议约定竣工时间（合作协议约定开工时间加建设工期确定）","type"=>"text"],
                "record"=>['name'=>"备案或立项","type"=>"text"],
                "contract"=>['name'=>"施工合同","type"=>"text"],
                "scene"=>['name'=>"项目现场","type"=>"text"],
                "project_chief"=>['name'=>"业主联系人","type"=>"text"],
                "project_phone"=>['name'=>"联系人电话","type"=>"text"],
                "contact_person"=>['name'=>"项目联络人","type"=>"text"],
                "contact_phone"=>['name'=>"联络人电话","type"=>"text"],
            ],
            4=>[
                "title"=>['name'=>"项目名称","type"=>"text"],
                "code"=>['name'=>"项目代码","type"=>"text"],
                "address"=>['name'=>"项目建设地点","type"=>"text"],
                "nature"=>['name'=>"项目建设性质","type"=>"text"],
                "project_type"=>['name'=>"项目类别","type"=>"text"],
                "content"=>['name'=>"主要建设内容及规模","type"=>"text"],
                "plan_onstream_time"=>['name'=>"拟开工时间","type"=>"text"],
                "plan_complete_time"=>['name'=>"拟建成时间","type"=>"text"],
                "investment"=>['name'=>"项目估算总投资（万元）","type"=>"text"],
                "owner"=>['name'=>"业主单位","type"=>"text"],
                "project_chief"=>['name'=>"业主联系人","type"=>"text"],
                "project_phone"=>['name'=>"联系人电话","type"=>"text"],
                "duty_unit"=>['name'=>"责任单位","type"=>"text"],
                "contact_person"=>['name'=>"责任人","type"=>"text"],
                "contact_phone"=>['name'=>"责任人电话","type"=>"text"],
            ],
            5=>[
                "title"=>['name'=>"项目名称","type"=>"text"],
                "content"=>['name'=>"主要建设内容及规模","type"=>"text"],
                "investment"=>['name'=>"总额度（万元）","type"=>"text"],
                "warehousingtitle"=>['name'=>"入库项目名称","type"=>"text"],
                "address"=>['name'=>"选址","type"=>"text"],
                "reply"=>['name'=>"入库状态","type"=>"text"],
                "warehousingTime"=>['name'=>"入库时间","type"=>"text"],
                "integrationRemark"=>['name'=>"存在的问题","type"=>"text"],
                "initial_investment_time"=>['name'=>"启报投资时间","type"=>"text"],
                "planned_production_time"=>['name'=>"计划投产时间（报竭时间）","type"=>"text"],
                "duty_unit"=>['name'=>"责任单位","type"=>"text"],
                "duty_person"=>['name'=>"责任人","type"=>"text"],
                "project_chief"=>['name'=>"业主联系人","type"=>"text"],
                "project_phone"=>['name'=>"联系人电话","type"=>"text"],
                "contact_person"=>['name'=>"项目联络人","type"=>"text"],
                "contact_phone"=>['name'=>"联络人电话","type"=>"text"],
            ],
            6=>[
                "title"=>['name'=>"企业名称","type"=>"text"],
                "enterprise"=>['name'=>"企业类型","type"=>"text"],
                "duty_unit"=>['name'=>"责任单位","type"=>"text"],
                "duty_person"=>['name'=>"责任人","type"=>"text"],
            ],
        ];

        if($detail['library']==6){
            if($detail['menuTop'] == 1){
                $addField = [
                    "leader"=>['name'=>"县级领导","type"=>"text"],
                    "industry"=>['name'=>"所属行业","type"=>"text"],
                    "product"=>['name'=>"主要产品","type"=>"text"],
                    "revenue"=>['name'=>"累计应税收入（万元）","type"=>"text"],
                    "gauge"=>['name'=>"拟进规时间","type"=>"text"],
                ];
            }else{
                $addField = [
                    "chargeman"=>['name'=>"组长","type"=>"text"],
                    "member"=>['name'=>"成员","type"=>"text"],
                    "remark"=>['name'=>"备注","type"=>"text"],
                ];
            }
            $addFieldend = [
                "liaison"=>['name'=>"联络员及电话","type"=>"text"],
                "operation"=>['name'=>"运营状况","type"=>"text"],
                "visit_remark"=>['name'=>"存在的问题","type"=>"text"],
            ];
            $field[$detail['library']] = array_merge($field[$detail['library']],$addField,$addFieldend);
        }

        $this->assign("field",$field[$detail['library']]);

        return $this->fetch("basic");
    }

    /**
     * 查看点评信息
     */
    public function index(){
        //查询项目
        $title = Db::name("project_pro")->where(['id'=>$_GET['pro_id']])->value('title');
        $this->assign("title",$title);
        if(!empty($_GET['type'])){
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;

                $where['pro_id'] = $_GET['pro_id'];
                $where['content'] =  ['neq',''];
                $where['pid'] = 0;
                $list = Db::name("project_pro_comment")->where($where)->limit($start,$limit)->order("id desc")->select();

                foreach ($list as $k=>$v){
                    if($v['type']==1){
                        $name = Db::name("account")->where(['id'=>$v['add_id']])->value("account");
                    }else{
                        $name = Db::name("project_account")->where(['id'=>$v['add_id']])->value("name");
                    }

                    $list[$k]['username'] = $name;
                }

                $count = Db::name("project_pro_comment")->where($where)->count();
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }
        return $this->fetch("index");
    }


    /**
     * 形象进度视图
     */
    public function detail(){
        if(!empty($_REQUEST['id'])){
            $id = $_REQUEST['id'];
            $detail = Db::name("project_pro_comment")->find($id);
            $detail['images'] = explode(";",$detail['images']);
            $this->assign('detail',$detail);
        }
        return $this->fetch("detail");
    }

    /**
     * 形象进度视图
     */
    public function showDetail(){
        if(!empty($_REQUEST['id'])){
            $id = $_REQUEST['id'];
            $detail = Db::name("project_pro_comment")->find($id);
            $detail['images'] = explode(";",$detail['images']);
            $this->assign('detail',$detail);

            $library = $_REQUEST['library'];
            $this->assign('library',$library);
        }
        return view("detail");
    }

    /*
     * 点评删除
     */
    public function del(){
        try{
            $id = $_POST['id'];
            $list = Db::name("project_pro_comment")->alias("a")
                ->join("project_pro b","a.pro_id = b.id")->where(['a.id'=>['in',$id]])
                ->field("b.title,a.id")->select();
            $res = Db::name("project_pro_comment")->where(['id'=>['in',$id]])->delete();
            if(empty($res)){
                throw new Exception("删除失败");
            }
            $common = new Common();
            foreach ($list as $k=>$v){
                $log['remark'] = "【工程管理】,账号【" . Session::get("user_name") . "】删除项目【".$v['title']."】的点评信息,关联id【" . $v['id'] . "】";
                $common->addLog($log);
            }
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

}